<template>
	<sw-common-frame :count="dataCount" :loading="loading">
		<template slot="button">
			<el-button size="small" type="primary" icon="el-icon-plus" @click="handleOpenDialog()">新增</el-button>
		</template>
		<template slot="query">
			<el-form size="small" inline>
				<el-form-item>
					<el-input v-model="queryData.name" clearable placeholder="请输入试卷名称" style="width: 180px"></el-input>
				</el-form-item>
				<el-form-item>
					<el-select v-model="queryData.paperType" clearable placeholder="试卷类型" style="width: 102px">
						<el-option label="普通试卷" value="1"></el-option>
						<el-option label="随机试卷" value="2"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-select v-model="queryData.permission" clearable placeholder="管理权限" style="width: 102px">
						<el-option label="完全私有" value="1"></el-option>
						<el-option label="只读共享" value="2"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-select v-model="queryData.status" clearable placeholder="试卷状态" style="width: 102px">
						<el-option label="正常" value="1"></el-option>
						<el-option label="禁用" value="2"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="el-icon-search" @click="queryTableData()">查询</el-button>
				</el-form-item>
			</el-form>
		</template>
		<el-table slot="table" :data="tableData" height="100%" stripe>
			<el-table-column label="排序" type="index" width="50" align="center"></el-table-column>
			<el-table-column label="试卷名称" prop="name" min-width="230">
				<el-link :underline="false" slot-scope="{ row }" @click="handleTestPaperDetail(row)">{{ row.name }}</el-link>
			</el-table-column>
			<el-table-column label="试卷分类" prop="sysClassify.name" min-width="90"></el-table-column>
			<el-table-column label="试卷类型" min-width="80" align="center">
				<template slot-scope="{ row }">
					<font color="#409EFF" v-if="row.paperType === '1'">普通试卷</font>
					<font color="#67C23A" v-if="row.paperType === '2'">随机试卷</font>
				</template>
			</el-table-column>
			<el-table-column label="试卷状态" min-width="90" align="center">
				<template slot-scope="{ row }">
					<el-tag size="medium" type="success" v-if="row.status === '1'">正常</el-tag>
					<el-tag size="medium" type="warning" v-if="row.status === '2'">禁用</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="总分" prop="totalScore" min-width="80" align="center" />
			<el-table-column label="访问权限" min-width="90" align="center">
				<template slot-scope="{ row }">
					<el-tag size="medium" type="success" v-if="row.permission === '1'">完全私有</el-tag>
					<el-tag size="medium" type="warning" v-if="row.permission === '2'">只读共享</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="创建时间" prop="createTime" min-width="160"></el-table-column>
			<el-table-column label="操作" width="186">
				<el-button-group slot-scope="{ row }">
					<el-button size="mini" type="info" @click="handleTestPaperDetail(row.id)">详情</el-button>
					<el-button size="mini" type="warning" @click="handleOpenDialog(row.id)">编辑</el-button>
					<el-button size="mini" type="danger" @click="handleDeleteData(row)">删除</el-button>
				</el-button-group>
			</el-table-column>
		</el-table>
		<sw-pagination slot="page" :total="dataCount" :page.sync="queryData.page" :limit.sync="queryData.limit" @change="queryTableData()" />
		<form-dialog v-model="formDialog" :data-id="dataId" @refresh="queryTableData()"></form-dialog>
		<subject-select v-model="selectDialog" :list="subjectDatas" :chapter-id="chapterId" />
		<paper-detail v-model="detailDialog" :paper-id="paperId"></paper-detail>
	</sw-common-frame>
</template>

<script>
import formDialog from './form';
import paperDetail from './detail';
import subjectSelect from './select';
import listMixins from '@/mixins/listMixins';
import { getTopicLibraryList } from '@/api/exam/testQuestionBank';
import { queryPapersList, deletePapers } from '@/api/exam/testPaper';
export default {
	mixins: [listMixins],
	provide() {
		return { subjectBanks: () => this.subjectBankData };
	},
	components: { formDialog, paperDetail, subjectSelect },
	data() {
		return {
			pageName: '试卷',
			subjectBankData: [],
			selectDialog: false,
			detailDialog: false,
			subjectDatas: [],
			chapterId: '',
			paperId: ''
		};
	},
	computed: {
		listFunction() {
			return eval(queryPapersList);
		},
		deleteFunction() {
			return eval(deletePapers);
		}
	},
	mounted() {
		this.getSubjectBankData();
		this.$bus.$on('selectSubject', (data) => {
			let subjects = data.testSubjectVO || [];
			this.chapterId = data.id || data.mark;
			this.subjectDatas = subjects;
			this.selectDialog = true;
		});
	},
	destroyed() {
		this.$bus.$off('selectSubject');
	},
	methods: {
		//获取题库数据
		async getSubjectBankData() {
			let params = { page: 1, limit: 9999, status: 1 };
			let result = await getTopicLibraryList(params).then((res) => res.data);
			if (result.code === '0') {
				this.subjectBankData = result.data || [];
			}
		},

		//查看试卷详情
		handleTestPaperDetail(id) {
			this.detailDialog = true;
			this.paperId = id;
		}
	}
};
</script>

<style lang="scss" scoped></style>
